package com.fitmanagement.controller;


import com.fitmanagement.entity.Exercises;
import com.fitmanagement.entity.Users;
import com.fitmanagement.service.IExercisesService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 虚空
 * @since 2024-10-25
 */
@RestController
@RequestMapping("/exercises")
@RequiredArgsConstructor // 自动注入
@CrossOrigin(origins = "http://localhost:5173") // 前端的地址
public class ExercisesController {
  private final IExercisesService exercisesService;


  //查询所有
    @GetMapping("/getAll")
    public List<Map<String, Object>> getAll(){
        List<Map<String, Object>> maps = exercisesService.listMaps();
        return maps;
    }


    //删除
    @DeleteMapping("/delete/{id}")
    public Boolean Delete(@PathVariable("id") int id) {

        Boolean result = exercisesService.removeByexercisesId(id);
        if (result) {
            System.out.println("User with ID: " + id + " deleted successfully.");
        } else {
            System.out.println("Failed to delete user with ID: " + id);
        }
        return result;
    }
    //模糊查询姓名
    @GetMapping("/Search")
    public List<Map<String, Object>> Search(@RequestParam("exercisename") String exercisename) {
        return exercisesService.SearchByName(exercisename);
    }
    @PostMapping("/add")
    public Boolean add(@RequestBody Exercises exercises) {
        System.out.println("exercises = " + exercises);

        exercises.setCreatedAt(LocalDateTime.now());
        exercises.setUpdatedAt(LocalDateTime.now());
        return exercisesService.save(exercises);
    }
    @GetMapping("/{id}")
    public Exercises getId(@PathVariable("id") int id) {
        return exercisesService.getById(id);
    }
    @PutMapping("/update/{id}")
    public Boolean updateStatus(@PathVariable Integer id, @RequestBody Exercises exercises) {
        Integer status = exercises.getStatus(); // 获取状态

        // 更新操作，假设 `updateStatusById` 方法只更新 `status`
        return exercisesService.updateStatusById(id, status);
    }

}
